<template>
  <div class="resource-list">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <el-form :inline="true" :model="form" ref="myForm" class="demo-form-inline">
          <el-form-item label="资源名称" prop="name">
            <el-input v-model="form.name" clearable placeholder="资源名称"></el-input>
          </el-form-item>
          <el-form-item label="资源路径" prop="url">
            <el-input v-model="form.url" clearable placeholder="资源路径"></el-input>
          </el-form-item>
          <el-form-item label="资源分类"  prop="categoryId">
            <el-select v-model="form.categoryId" clearable  placeholder="资源分类">
                <!-- <el-option label="不限" value="-1"></el-option> -->
              <el-option v-for="item in categories"
               :label="item.name" :key="item.id" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="onSubmit" :disabled="loading">查询</el-button>
            <el-button  @click="onReset">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
      <div>
        <el-button @click="addResource">添加</el-button>
        <el-table :data="resources" style="width: 100%" v-loading="loading">
          <el-table-column type="index" label="编号"> </el-table-column>
          <el-table-column prop="name" label="资源名称"> </el-table-column>
          <el-table-column prop="url" label="资源路径"> </el-table-column>
          <el-table-column prop="description" label="描述"> </el-table-column>
          <!-- 设置作用域，需要使用作用于插槽 -->
          <el-table-column label="添加时间">
            <template slot-scope="scope">
                <span>{{ scope.row.createdTime | dateFormat }}</span>
            </template>
          </el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button size="mini" @click="handleEdit(scope.row)">编辑</el-button>
              <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
         <div class="block">
            <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="form.current"
            :page-sizes="[10, 20, 30, 40]"
            :page-size="10"
            layout="total, sizes, prev, pager, next, jumper"
            :total="totalCount"
            :disabled="loading"
            >
            </el-pagination>
        </div>
      </div>
    </el-card>
    <el-dialog
      title="提示"
      :visible.sync="dialogVisible"
      width="50%">
       <create-or-edit @handleDisable='handleDisable' :isEdit="isEdit" :resourceId="editId"></create-or-edit>
    </el-dialog>
  </div>
</template>

<script>
import CreateOrEdit from './CreateOrEdit.vue'
import { getResourcePages, getResourceCategories, deleteResourceById } from '@/services/resource'

export default {
  name: 'List',
  components: {
    CreateOrEdit
  },
  data () {
    return {
      editId: null,
      isEdit: true,
      dialogVisible: false,
      form: {
        name: '',
        categoryId: '',
        url: '',
        current: 1,
        // 每页显示的数据条数
        size: 10
      },
      // 数据总值
      totalCount: 0,
      // 存储资源列表数据
      resources: [],
      categories: [],
      currentPage1: 5,
      currentPage2: 5,
      currentPage3: 5,
      currentPage4: 4,
      loading: false
    }
  },
  created () {
    this.loadResouces()
    this.loadCategories()
  },
  methods: {
    handleDisable (type) {
      this.dialogVisible = false
      console.log(type)
      if (type === 1) {
        this.loadResouces()
      }
    },
    addResource () {
      this.dialogVisible = true
      this.isEdit = false
    },
    onReset () {
      this.$refs.myForm.resetFields();
    },
    async loadCategories () {
      const { data } = await getResourceCategories()
      if (data.code === '000000') {
        this.categories = data.data
      }
    },
    handleEdit (row) {
      this.isEdit = true
      this.dialogVisible = true
      this.editId = row.id
    },
    handleDelete (row) {
      this.$confirm('确定删除吗', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const { data } = await deleteResourceById(row.id)
        if (data.code === '000000') {
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
          this.loadResouces()
        } else {
          this.$message({
            type: 'error',
            message: '删除成功!'
          });
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    async loadResouces () {
      this.loading = true
      const { data } = await getResourcePages(this.form)
      if (data.code === '000000') {
        this.resources = data.data.records
        this.totalCount = data.data.total
        this.loading = false
      }
    },
    onSubmit () {
      this.form.current = 1
      this.loadResouces()
    },
    // 每页显示条数变化时触发
    handleSizeChange (val) {
      this.form.size = val
      // 由于修改了每页显示的条数，应当将页数归零
      this.form.current = 1
      this.loadResouces()
    },
    // 当前页发生变化
    handleCurrentChange (val) {
      console.log(`当前页: ${val}`);
      this.form.current = val
      this.loadResouces()
    }
  },
  filters: {
    // 日期过滤器
    dateFormat (date) {
      date = new Date(date)
      return `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()} ${date.getHours()}:${date.getMinutes()}:${date.getSeconds()} `
    }
  }
};
</script>

<style lang="scss" scoped>
.block{
    text-align: center;
    margin-top: 20px;
}
</style>
